// import { login, logout, getInfo } from '@/api/user'
// import { getToken, setToken, removeToken } from '@/utils/auth'
// import { resetRouter } from '@/router'

import { getProfile, login, getUserProfile } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'

export default {
  // 开启命名空间
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    // 将token存入到vuex中然后准备持久化存储
    updateToken(state, token) {
      state.token = token
      // 利用cookie持久化token
      setToken(token)
    },
    // 清空token和用户信息
    delToken(state) {
      state.token = ''
      state.userInfo = {}
    },
    // 保存用户信息
    setUserInfo(state, info) {
      state.userInfo = info
    }
  },
  actions: {
    // 获取token
    async userToken(context, data) {
      try {
        const res = await login(data)
        // console.log('res', res)
        context.commit('updateToken', res.data)
      } catch (error) {
        console.log(error)
      }
    },
    // 获取用户基本信息
    async getUserInfo(context) {
      try {
        const info = await getProfile()
        console.log('用户基本信息', info)
        const userPic = await getUserProfile(info.data.userId)
        console.log('userPic', userPic)
        context.commit('setUserInfo', { ...info.data, ...userPic.data })
        return info.data.roles.menus
      } catch (error) {
        console.log(error)
      }
    }
  }
}
